package utils

import (
	"encoding/json"
	"fmt"
	"time"

	"io/ioutil"

	"github.com/astaxie/beego/httplib"

	"strconv"

	"github.com/astaxie/beego"
)

type Jsondata struct {
	Code int    `json:"code"`
	Msg  string `json:"msg"`
}

/**
* 收币通知
* https://api.jpush.cn/v3/push 极光推送
* @uid 用户id
* @coinSx 币种简称
* @amount 接收金额
* @transferId
 */
func AdminReceived(billid int, status int, errdet string) Response {
	var response Response

	var url1 = beego.AppConfig.String("admin_url") + "/api/notice/add"
	var jsondata Jsondata

	var key = beego.AppConfig.String("admin_key")

	Status := strconv.Itoa(status)
	Billid := strconv.Itoa(billid)
	Timestamp := strconv.Itoa(int(time.Now().UnixNano() / 1e6))

	str := "msg=" + errdet + "&order_id=" + Billid + "&status=" + Status + "&timestamp=" + Timestamp + "&key=" + key
	post := httplib.Post(url1)
	post.Header("authorization", GetMD5(str))
	post.Header("Content-Type", "application/form-data")

	post.Param("status", Status)
	post.Param("msg", errdet)
	post.Param("order_id", Billid)
	post.Param("timestamp", Timestamp)
	res, _ := post.DoRequest()

	defer res.Body.Close()

	input, _ := ioutil.ReadAll(res.Body)
	fmt.Println(string(input))
	json.Unmarshal([]byte(input), &jsondata)

	beego.Debug("提交数据", jsondata)
	if jsondata.Code != 200 {
		response.Code = 1
		response.Msg = jsondata.Msg
	}
	return response
}

//func init() {
//	fmt.Println(AdminReceived(11, 1, "sucess"))
//}
